//
//  UIImage+Extension.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/23.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit

extension UIImage{

    class func jieQuScreen() -> UIImage {
        
        //添加被处理毛玻璃效果的背景图  截取屏幕
        
        //截屏
        let window = UIApplication.shared.keyWindow
        //开启图片的上下文
        UIGraphicsBeginImageContextWithOptions((window?.frame.size)!, false, 0)
        //将当前的window绘制到图形的上下文
        window?.drawHierarchy(in: (window?.frame)!, afterScreenUpdates: true)
        //从图片的上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图片的上下文
        UIGraphicsEndImageContext()
        
        return image!
        
    }
    
    //将图片缩放到指定的宽度
    func suoFangImage(width : CGFloat) -> UIImage {
        
        let imageWidth = self.size.width
        let imageHeight = self.size.height
        
        //需要判断，如果图片的宽度，小于图片的宽度，就不需要缩放
        if imageWidth < width {
            
            return self
            
        }
        
        let suoFangImageHeight = imageHeight / imageWidth * width
        let suoFangImageFrame = CGRect(x: 0, y: 0, width: width, height: suoFangImageHeight)
        
        //开启图片的上下文
        UIGraphicsBeginImageContextWithOptions(suoFangImageFrame.size, false, 0)
        
        //将图片绘制到上下文中
        self.draw(in: suoFangImageFrame)
        
        //从上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭图片的上下文
        UIGraphicsEndImageContext()
        return image!
        
        
    }

}
